package com.qys.livesMall.finance.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.finance.dao.   FinanceUserAccountDetailDao;
import com.qys.livesMall.finance.event.UserIncomeDayEvent;
import com.qys.livesMall.finance.mapper.   FinanceUserAccountDetailMapper;
import com.qys.livesMall.finance.model.dto.AccountDdetailSearch;
import com.qys.livesMall.finance.model.dto.AccountDetailSearch;
import com.qys.livesMall.finance.model.dto.AccountHandle;
import com.qys.livesMall.finance.model.dto.AccountInOut;
import com.qys.livesMall.finance.model.dto.user.UserIncomeDayDTO;
import com.qys.livesMall.finance.model.entity.user.FinanceUserAccount;
import com.qys.livesMall.finance.model.entity.user.FinanceUserAccountAfter;
import com.qys.livesMall.finance.model.entity.user.FinanceUserAccountDetail;
import com.qys.livesMall.finance.service.   FinanceUserAccountDetailService;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.Paging;
import com.qys.livesMall.framework.view.QysPage;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 店铺余额明细 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-06
 */
@Service
public class FinanceUserAccountDetailServiceImpl implements FinanceUserAccountDetailService {

    @Autowired
    private    FinanceUserAccountDetailDao userAccountDetailDao;

    @Autowired
    private    FinanceUserAccountDetailMapper userAccountDetailMapper;

    @Resource
    private ApplicationContext applicationContext;

    @Override
    public void add(AccountHandle handle,    FinanceUserAccount balance) {
        FinanceUserAccountDetail detail=new FinanceUserAccountDetail();
        detail.setUserId(balance.getUserId());
        detail.setShopId(handle.getShopId());
        detail.setBalanceId(balance.getId());
        detail.setBusinessNo(handle.getBusinessNo());
        detail.setPayMethod(handle.getPayMethod());
        detail.setType(handle.getTradeTypeEnum().getType());
        detail.setInOut(handle.getTradeTypeEnum().getInOut());
        detail.setCategory(handle.getBusinessType().getType());
        detail.setCode(handle.getBusinessType().getCode());
        detail.setAmountIncurred(handle.getAmountIncurred());
        detail.setExpectedAmount(handle.getAmountIncurred());
        detail.setDescription(handle.getDescription());
        FinanceUserAccountAfter after=BeanProUtils.copyProperties(balance,    FinanceUserAccountAfter::new);
        detail.setPayAfter(JSON.toJSONString(after));
        detail.setPayDate(LocalDateTime.now());
        detail.setSerialNumber(handle.getSerialNumber());
        if (null==SecurityUtils.getUsername()){
            detail.setOperator("sys");
        }else {
            detail.setOperator(SecurityUtils.getUsername());
        }

        userAccountDetailDao.save(detail);
        UserIncomeDayDTO userIncomeDayDTO=new UserIncomeDayDTO();
        userIncomeDayDTO.setTodayincome(detail.getAmountIncurred());
        userIncomeDayDTO.setType(0);
        userIncomeDayDTO.setRelevanceId(detail.getUserId());
        userIncomeDayDTO.setInOut(detail.getInOut());
        userIncomeDayDTO.setCode(detail.getCode());
        applicationContext.publishEvent(new UserIncomeDayEvent(userIncomeDayDTO));
    }

    @Override
    public void addOtherLog(AccountHandle handle,    FinanceUserAccount balance){
        FinanceUserAccountDetail detail=new FinanceUserAccountDetail();
        detail.setUserId(balance.getUserId());
        detail.setShopId(handle.getShopId());
        detail.setBalanceId(balance.getId());
        detail.setBusinessNo(handle.getBusinessNo());
        detail.setPayMethod(handle.getPayMethod());
        detail.setType(handle.getTradeTypeEnum().getType());
        detail.setInOut(handle.getTradeTypeEnum().getInOut());
        detail.setCategory(handle.getBusinessType().getType());
        detail.setCode(handle.getBusinessType().getCode());
        detail.setAmountIncurred(handle.getAmountIncurred());
        detail.setExpectedAmount(handle.getFreezeAmount());
        detail.setDescription(handle.getDescription());
        FinanceUserAccountAfter after=BeanProUtils.copyProperties(balance,    FinanceUserAccountAfter::new);
        detail.setPayAfter(JSON.toJSONString(after));
        detail.setPayDate(LocalDateTime.now());
        detail.setSerialNumber(handle.getSerialNumber());
        if (null==SecurityUtils.getUsername()){
            detail.setOperator("sys");
        }else {
            detail.setOperator(SecurityUtils.getUsername());
        }
        userAccountDetailDao.save(detail);
    }

    @Override
    public QysPage<FinanceUserAccountDetail> list(AccountDetailSearch search) {
        Paging<FinanceUserAccountDetail> page=userAccountDetailMapper.search(new Paging<>(search));

        return QysPage.to(page);
    }

    @Override
    public AccountInOut sum(AccountDetailSearch search) {
        return userAccountDetailMapper.sum(search);
    }

    @Override
    public List<FinanceUserAccountDetail> getbyCode(List<String> codes) {
        LambdaQueryWrapper<FinanceUserAccountDetail> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(FinanceUserAccountDetail::getCode,codes);
        List<FinanceUserAccountDetail> list=userAccountDetailMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public QysPage<FinanceUserAccountDetail> listPage(AccountDdetailSearch search) {
        LambdaQueryWrapper<FinanceUserAccountDetail> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(FinanceUserAccountDetail::getBalanceId,search.getBalanceId());
        queryWrapper.in(null!=search.getUserIds(),FinanceUserAccountDetail::getUserId,search.getUserIds());
        queryWrapper.eq(null != search.getCategory(),FinanceUserAccountDetail::getCategory,search.getCategory());
        queryWrapper.eq(null != search.getInOut(),FinanceUserAccountDetail::getInOut,search.getInOut());
        queryWrapper.orderByDesc(FinanceUserAccountDetail::getPayDate,FinanceUserAccountDetail::getId);
        Page<FinanceUserAccountDetail> page=userAccountDetailMapper.selectPage(new Page<>(search.getPageNum(), search.getPageSize()), queryWrapper);
        return QysPage.build(search).setRecords(page.getRecords()).setTotal(page.getTotal());
    }

    @Override
    public List<String> getOrderSkuNameByOrderNo(String businessNo) {
        return userAccountDetailMapper.getOrderSkuNameByOrderNo(businessNo);
    }
}
